# 该程序，只用来创建授权文件。为单独工具

from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
import base64

end_time = "2024-5-31 23:59:59"

# 公钥
rsa_pub_2048 = """\
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4gL61IIZcEzR5S9553PyQHM1PIWavTRdGSs4O6WYCYt7ZKhPg7WxqiTjC4wen0ks2Y4VhBvcQU7e2uS3x6hXEfd/7B5jzQklqxfsHf3Pf9b8hR8RyN9llApTH4lGKdZ7wAoAacjeZhZCQX5w0HxUhM5scWAOiI4EL7P8Zg6JPLWWrdVJ2iIZ0cww+Gl7kIqti/wZiehPNCGRfTjJIFo1V8VNWcNGlo7jNMCBX+fTW5Gg4lsGxpeTiDwf5fD1+0byFNmax3eX7PXloLwKuKI1kYiPNZClXKxD/Ht6td2I57VO78oePvGQpnYtMcRUBNlqCkauvhBCzwQHo8cFvluMbQIDAQAB
-----END PUBLIC KEY-----
"""

# 私钥
rsa_key = """\
-----BEGIN RSA PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDiAvrUghlwTNHlL3nnc/JAczU8hZq9NF0ZKzg7pZgJi3tkqE+DtbGqJOMLjB6fSSzZjhWEG9xBTt7a5LfHqFcR93/sHmPNCSWrF+wd/c9/1vyFHxHI32WUClMfiUYp1nvACgBpyN5mFkJBfnDQfFSEzmxxYA6IjgQvs/xmDok8tZat1UnaIhnRzDD4aXuQiq2L/BmJ6E80IZF9OMkgWjVXxU1Zw0aWjuM0wIFf59NbkaDiWwbGl5OIPB/l8PX7RvIU2ZrHd5fs9eWgvAq4ojWRiI81kKVcrEP8e3q13YjntU7vyh4+8ZCmdi0xxFQE2WoKRq6+EELPBAejxwW+W4xtAgMBAAECggEBAIrh6eDMUXWAvzpmgjKIPUT9hqk6MGwz0UG2HUgJ6fNzks+33aTB8frrz9k4sViQTs8hUHdt51r6bFd2VLi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wzv9vZWsveA+2xyr7OZp5xQeMZX6DaJlXx54krlpOYgukFLkiU2ScvNqfP4ZcYlc=
-----END RSA PRIVATE KEY-----
"""


# 加密函数
def encrypt(pubkey, text):
    cipher_rsa = PKCS1_v1_5.new(RSA.importKey(pubkey))
    encrypt_text = cipher_rsa.encrypt(bytes(text, 'utf-8'))
    cipher_text = base64.b64encode(encrypt_text).decode()
    # cipher_text = cipher_rsa.encrypt(bytes(text, 'utf-8'))
    return cipher_text

# 解密函数
def decrypt(privatekey, text):
    cipher_rsa = PKCS1_v1_5.new(RSA.import_key(privatekey))
    cipher_text = cipher_rsa.decrypt(base64.b64decode(text),0)
    # cipher_text = cipher_rsa.decrypt(text,0)
    return cipher_text


if __name__ == '__main__':
    info = encrypt(rsa_pub_2048, end_time)   # 生成激活文件
    filename = '/src/catchdeb.LICENSE'
    with open(filename, 'w', encoding='utf-8') as f:
        f.write(info)
        print("授权文件写入成功： ",filename)
    # print(decrypt(rsa_key, info).decode(encoding='utf-8'))